/*
 *  Copyright (c) 2011 Leibniz Institute of Plant Genetics and Crop Plant Research (IPK), Gatersleben, Germany.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the GNU Lesser Public License v2.1
 *  which accompanies this distribution, and is available at
 *  http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 *  Contributors:
 *      Leibniz Institute of Plant Genetics and Crop Plant Research (IPK), Gatersleben, Germany - RMI Client, FileChooser and WebDAV
 */
package de.ipk_gatersleben.bit.bi.edal.publication;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Locale;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.ToolTipManager;

public class PropertyLoader {

	public static final String PROGRAM_NAME = "IPK Data Publication System";

	public static Image EDAL_LOGO = null;

	public static Image EDAL_ICON = null;

	public static Properties props;

	public static Path RUNNING_PATH = null;

	public static final Color HEADER_FOOTER_COLOR = new Color(210, 210, 210);

	public static final Color MAIN_BACKGROUND_COLOR = Color.WHITE;

	public static final Color LABEL_COLOR = new Color(0, 132, 184);

	public static final Color MAIN_FONT_COLOR = new Color(102, 102, 102);

	public static final int DEFAULT_FONT_SIZE = 14;

	public static final Font DEFAULT_FONT = new Font(Font.SANS_SERIF, 0,
			PropertyLoader.DEFAULT_FONT_SIZE);

	public static final int THREE_TIMES_SIZE = 3 * DEFAULT_FONT_SIZE;

	public static final int PUBLISHER_PANEL_HEIGHT = 3 * DEFAULT_FONT_SIZE;

	public static final int AUTHOR_PANEL_HEIGHT = 10 * DEFAULT_FONT_SIZE;

	public static final int DESCRIPTION_PANEL_HEIGHT = 10 * DEFAULT_FONT_SIZE;

	public static final Dimension MINIMUM_DIM_PUBLICATION_FRAME = new Dimension(
			800, 800);

	public static AttributeLabel UPLOADPATH_LABEL, TITLE_LABEL,
			DESCRIPTION_LABEL, AUTHORS_LABEL, SUBJECTS_LABEL, LANGUAGE_LABEL,
			PUBLISHER_LABEL, EMBARGO_LABEL;

	private static void initGraphicComponents() {
		UPLOADPATH_LABEL = new AttributeLabel(
				PropertyLoader.props.getProperty("UPLOAD_LABEL"),
				PropertyLoader.props.getProperty("UPLOAD_TOOLTIP"));
		TITLE_LABEL = new AttributeLabel(
				PropertyLoader.props.getProperty("TITLE_LABEL"),
				PropertyLoader.props.getProperty("TITLE_TOOLTIP"));
		DESCRIPTION_LABEL = new AttributeLabel(
				PropertyLoader.props.getProperty("DESCRIPTION_LABEL"),
				PropertyLoader.props.getProperty("DESCRIPTION_TOOLTIP"));
		AUTHORS_LABEL = new AttributeLabel(
				PropertyLoader.props.getProperty("AUTHORS_LABEL"),
				PropertyLoader.props.getProperty("AUTHORS_TOOLTIP"));
		SUBJECTS_LABEL = new AttributeLabel(
				PropertyLoader.props.getProperty("SUBJECTS_LABEL"),
				PropertyLoader.props.getProperty("SUBJECTS_TOOLTIP"));
		LANGUAGE_LABEL = new AttributeLabel(
				PropertyLoader.props.getProperty("LANGUAGE_LABEL"),
				PropertyLoader.props.getProperty("LANGUAGE_TOOLTIP"));
		PUBLISHER_LABEL = new AttributeLabel(
				PropertyLoader.props.getProperty("PUBLISHER_LABEL"),
				PropertyLoader.props.getProperty("PUBLISHER_TOOLTIP"));
		EMBARGO_LABEL = new AttributeLabel(
				PropertyLoader.props.getProperty("EMBARGO_LABEL"),
				PropertyLoader.props.getProperty("EMBARGO_TOOLTIP"));
	}

	private static void loadEdalLogos() {
		EDAL_LOGO = new ImageIcon(
				PropertyLoader.class.getResource("edal_scaled.png")).getImage();
		EDAL_ICON = new ImageIcon(
				PropertyLoader.class.getResource("edal_icon.png")).getImage();

	}

	private static void loadProperties() {

		String propertyFile = "properties.txt";

		props = new Properties();

		try {
			props.load(new InputStreamReader(PropertyLoader.class
					.getResourceAsStream(propertyFile), "UTF-8"));
		} catch (IOException | NullPointerException e) {

			JOptionPane.showMessageDialog(PublicationModul.getFrame(),
					"Unable to load property file '" + propertyFile + "' : "
							+ e.getMessage(), "ERROR",
					JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		}
	}

	private static void loadRunnigPath() {

		try {
			RUNNING_PATH = Paths.get(PropertyLoader.class.getProtectionDomain()
					.getCodeSource().getLocation().toURI());
		} catch (Exception e) {
			RUNNING_PATH = Paths.get(System.getProperty("user.home"));
		}
	}

	public static void main(String[] args) {

		Locale.setDefault(Locale.ENGLISH);

		loadProperties();
		loadRunnigPath();
		loadEdalLogos();
		initGraphicComponents();

		ToolTipManager.sharedInstance().setInitialDelay(0);

	}
}